﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.Reflection;
using Cn.Cellula.Biz;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using System.Collections.Generic;
using System.Text;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Product {
    public partial class AddAtlInfo : PageSupport {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        ProductEditBiz peb = new ProductEditBiz();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
            }
        }
        private void InitForm() {
            ProductActor pa = peb.PopFlow(Session);
            if (pa != null) {
                this.lab_CategoryName.Text = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, CodeTypeDef.Category.Product, pa.CategoryKey, " / ");
                this.hid_CategoryKey.Value = pa.CategoryKey;
                try {
                    TProductEO peo = biz.Load<TProductEO>(pa.ProductKey);
                    this.lab_ProductName.Text = peo.ProductName;
                    this.hid_ProductKey.Value = pa.ProductKey;
                } catch {
                    SiteInfo.Send(Page, "错误", "没有指定有效的产品");
                    return;
                }
            } else {
                SiteInfo.Send(Page, "错误", "没有选择有效的产品分类");
                return;
            }

            this.lab_AtlForm.Text = ClientBiz.LoadProductAtlEditForm(this.hid_CategoryKey.Value, null);
        }

        private void DoSave() {
            IList<VProductAtlEO> list = biz.LoadProductAtlByCategory(this.hid_CategoryKey.Value);
            IList<TProductAtlDataEO> dataList = new List<TProductAtlDataEO>();
            foreach (VProductAtlEO eo in list) {
                if (String.IsNullOrEmpty(Request.Form[eo.AtlKey])) continue;
                TProductAtlDataEO data = new TProductAtlDataEO();
                data.AtlKey = eo.AtlKey;
                data.DataValue = Request.Form[eo.AtlKey];
                data.ProductKey = this.hid_ProductKey.Value;
                dataList.Add(data);
            }
            biz.SaveProductAtlData(dataList);
        }
         protected void btn_Save_Click(object sender, EventArgs e) {
            try {
                DoSave();
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "保存扩展属性时出现错误");
                return;
            }
            peb.GoNext(Request, Response);
        }

        protected void btn_Finish_Click(object sender, EventArgs e) {
             try {
                 DoSave();
             } catch(Exception ex) {
                 log.Error(ex.Message);
                 SiteInfo.Send(Page, TxtDef.ERROR, "保存扩展属性时出现错误");
                 return;
             }
             DoSave();
            peb.Finish(Response);
        }
    }
}
